package com.ruoyi.project.mahjong.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.TConsumptionOrder;
import com.ruoyi.project.mahjong.service.ITConsumptionOrderService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 消费清单Controller
 *
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/mahjong/order")
public class TConsumptionOrderController extends BaseController
{
    @Autowired
    private ITConsumptionOrderService tConsumptionOrderService;

    /**
     * 查询消费清单列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(TConsumptionOrder tConsumptionOrder)
    {
        startPage();
        List<TConsumptionOrder> list = tConsumptionOrderService.selectTConsumptionOrderList(tConsumptionOrder);
        return getDataTable(list);
    }

    /**
     * 导出消费清单列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:order:export')")
    @Log(title = "消费清单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TConsumptionOrder tConsumptionOrder)
    {
        List<TConsumptionOrder> list = tConsumptionOrderService.selectTConsumptionOrderList(tConsumptionOrder);
        ExcelUtil<TConsumptionOrder> util = new ExcelUtil<TConsumptionOrder>(TConsumptionOrder.class);
        return util.exportExcel(list, "order");
    }

    /**
     * 获取消费清单详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:order:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tConsumptionOrderService.selectTConsumptionOrderById(id));
    }

    /**
     * 新增消费清单
     */
    @PreAuthorize("@ss.hasPermi('mahjong:order:add')")
    @Log(title = "消费清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TConsumptionOrder tConsumptionOrder)
    {
        return toAjax(tConsumptionOrderService.insertTConsumptionOrder(tConsumptionOrder));
    }

    /**
     * 修改消费清单
     */
    @PreAuthorize("@ss.hasPermi('mahjong:order:edit')")
    @Log(title = "消费清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TConsumptionOrder tConsumptionOrder)
    {
        return toAjax(tConsumptionOrderService.updateTConsumptionOrder(tConsumptionOrder));
    }

    /**
     * 删除消费清单
     */
    @PreAuthorize("@ss.hasPermi('mahjong:order:remove')")
    @Log(title = "消费清单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int flag=0;
        for(Long id:ids) {
            TConsumptionOrder order = tConsumptionOrderService.selectTConsumptionOrderById(id);
            if(order.getState().equals("1")){
                flag=1;
                break;

            }
        }
       if((flag==1)){
           return AjaxResult.error("存在已结算清单，请查看！");
       }
        for(Long id:ids) {
            TConsumptionOrder order = tConsumptionOrderService.selectTConsumptionOrderById(id);
            order.setState("1");
            order.setId(id);
            tConsumptionOrderService.updateTConsumptionOrder(order);
        }
        return AjaxResult.success();
    }
}
